/**
 * @author yuyangyang
 * @time 2015/2/27
 */

define(function (require) {
    var angular = require('angular');
    var $ = require('zepto');
    require('mobiscroll');
    require('./services');

    angular.module('controllers', ['services'])


        .controller('SportListCtrl', function ($scope, sportService) {
            sportService.getList(function (sportList) {
                $scope.sportList = sportList;
            });
        })


        .controller('SportNewCtrl', function ($scope, $location, sportService) {
            $scope.sport = {
                name: ''
            };

            $scope.submit = function () {
                sportService.addSport($scope.sport, function (sport) {
                    if (sport) {
                        $location.path('/');
                    }
                });
            };
        })

        .controller('MeetListCtrl', function ($scope, $routeParams, sportService, meetService) {
            sportService.getItem($routeParams.sportId, function (sport) {
                meetService.getList($routeParams.sportId, function (meetList) {
                    $scope.sport = sport;
                    $scope.meetList = meetList;
                });
            });
        })


        .controller('MeetNewCtrl', function ($scope, $routeParams, $location, sportService, meetService) {
            sportService.getItem($routeParams.sportId, function (sport) {
                $scope.sport = sport;
            });

            $scope.meet = {
                sport: $routeParams.sportId,
                title: '',
                content: '',
                startTime: '',
                place: ''
            };

            $('#calendar').mobiscroll().calendar({
                controls: ['calendar', 'time'],
                theme: 'mobiscroll',
                lang: 'zh',
                display: 'bottom',
                dateFormat: 'yyyy-mm-dd',
                minDate: new Date(),
                onSelect: function (date) {
                    $scope.startTime = date;
                }
            });

            $scope.submit = function () {
                meetService.addMeet($scope.meet, function (data) {
                    if (data) {
                        $location.path('/sport/' + $routeParams.sportId);
                    }
                });
            };

        })


        .controller('MeetDetailCtrl', function ($scope, $routeParams, meetService, joinService) {
            meetService.getItem($routeParams.meetId, function (data) {
                $scope.meet = data.meet;
                $scope.joinList = data.joinList;

                if ($scope.isLogin) {
                    joinService.getMessage({meet: $scope.meet._id}, function (join) {
                        if (join) {
                            $scope.isJoin = true;
                            $scope.join = join;
                        } else {
                            $scope.isJoin = false;
                        }
                    });
                }
            });


            $scope.join = function () {
                joinService.addJoin({meet: $scope.meet._id}, function (join) {
                    $scope.isJoin = join ? true : false;
                });
            };

            $scope.cancel = function () {
                joinService.cancel({joinId: $scope.join._id}, function (join) {
                    $scope.isJoin = false;
                });
            };
        })

        .controller('OwnerCtrl', function ($scope, $rootScope, $location, ownerService) {
            $scope.logout = function () {
                ownerService.logout(function (data) {
                    if (data) {
                        $rootScope.isLogin = false;
                        delete $rootScope.owner;
                        $location.path('/owner');
                    }
                });
            };
        })

        .controller('RegisterCtrl', function ($scope, $rootScope, $location, ownerService) {
            $scope.owner = {
                userName: '',
                password: '',
                realName: '',
                mobile: ''
            };

            $scope.submit = function () {
                ownerService.register($scope.owner, function (owner) {
                    if (owner) {
                        $rootScope.isLogin = true;
                        $rootScope.owner = owner;
                        $location.path('/owner');
                    }
                });
            };
        })

        .controller('LoginCtrl', function ($scope, $rootScope, $location, ownerService) {
            $scope.owner = {
                userName: '',
                password: ''
            };

            $scope.hasError = false;


            $scope.submit = function () {
                ownerService.login($scope.owner, function (owner) {
                    if (owner) {
                        $rootScope.isLogin = true;
                        $rootScope.owner = owner;
                        $location.path('/owner');
                    } else {
                        $scope.hasError = true;
                    }
                });
            };
        })

});


